package com.yl.project.lvyou.user.controller;

import java.util.Arrays;
import java.util.List;

import com.yl.common.utils.Md5Utils;
import com.yl.project.system.dict.domain.DictData;
import com.yl.project.system.dict.service.IDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yl.framework.aspectj.lang.annotation.Log;
import com.yl.framework.aspectj.lang.enums.BusinessType;
import com.yl.project.lvyou.user.domain.ScenicUser;
import com.yl.project.lvyou.user.service.IScenicUserService;
import com.yl.framework.web.controller.BaseController;
import com.yl.framework.web.domain.AjaxResult;
import com.yl.common.utils.poi.ExcelUtil;
import com.yl.framework.web.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author yl
 * @date 2025-03-13
 */
@Controller
@RequestMapping("/lvyou/user")
public class ScenicUserController extends BaseController
{
    private String prefix = "scenemanager/user";

    @Autowired
    private IScenicUserService scenicUserService;

    @Autowired
    private IDictDataService dictDataService;

    @RequiresPermissions("scenemanager:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    /**
     * 查询用户信息列表
     */
    @RequiresPermissions("scenemanager:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ScenicUser scenicUser)
    {
        startPage();
        List<ScenicUser> list = scenicUserService.selectScenicUserList(scenicUser);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @RequiresPermissions("scenemanager:user:export")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ScenicUser scenicUser)
    {
        List<ScenicUser> list = scenicUserService.selectScenicUserList(scenicUser);
        ExcelUtil<ScenicUser> util = new ExcelUtil<ScenicUser>(ScenicUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 新增用户信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户信息
     */
    @RequiresPermissions("scenemanager:user:add")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ScenicUser scenicUser)
    {
        ScenicUser user =scenicUserService.selectOneByUserName(scenicUser.getUsername());
        if(user==null){
            scenicUser.setPassword(Md5Utils.hash("123456"));
            scenicUserService.insertScenicUser(scenicUser);
            return toAjax(scenicUserService.insertScenicUser(scenicUser));
        }else{
            return error("用户名已存在，请注册!");
        }
    }

    /**
     * 修改用户信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ScenicUser scenicUser = scenicUserService.selectScenicUserById(id);
        mmap.put("scenicUser", scenicUser);
        if(scenicUser.getTravelPreferences()!=null){
            String[] travelPreferences=scenicUser.getTravelPreferences().split(",");
            List<String> travelPreferencesList = Arrays.asList(travelPreferences);
            mmap.put("travelPreferencesList", travelPreferencesList);
        }else{
            mmap.put("travelPreferencesList", "");
        }

        return prefix + "/edit";
    }

    /**
     * 修改保存用户信息
     */
    @RequiresPermissions("scenemanager:user:edit")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ScenicUser scenicUser)
    {
        return toAjax(scenicUserService.updateScenicUser(scenicUser));
    }

    /**
     * 删除用户信息
     */
    @RequiresPermissions("scenemanager:user:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(scenicUserService.deleteScenicUserByIds(ids));
    }
}
